package cc.lj1.test.authtest.web;

import cc.lj1.auth.annotation.*;
import cc.lj1.auth.helper.AuthUtils;
import cc.lj1.auth.services.AuthTokenService;
import cc.lj1.test.authtest.model.AnotherUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/another")
@RestController
public class AnotherTestController {

    @Autowired
    AuthTokenService authTokenService;

    @GetMapping
    public String hello() {
        return "another";
    }

    @GetMapping("/agent")
    public String testAgengType(@RequestAgentType String agentType) {
        return "agent type is " + agentType + ", " + AuthUtils.agentType();
    }

    @AuthenticationRequired("another")
    @GetMapping("/auth")
    public String helloWithAuth(@RequestCurrentUser AnotherUser user, @RequestAgentType String agentType) {
//        User user = (User) request.getAttribute(AuthProperties.USER_KEY);
        return "hello with auth [agentType = " + agentType + "]" + user.toString() + ", " + AuthUtils.currentUser().toString();
    }

    @AuthenticationRequired("another")
    @PermissionRequired("acl")
    @GetMapping("/acl")
    public String helloWithAcl(@RequestCurrentUser AnotherUser user) {
        return "hello with acl " + user.toString();
    }

    @AuthenticationRequired("another")
    @PermissionRequired("acl2")
    @GetMapping("/acl2")
    public String helloWithAcl2(@RequestCurrentUser AnotherUser user) {
        return "hello with acl2 " + user.toString();
    }

    @RequestMapping("/login")
    public String login(@RequestAgentType String agentType, @RequestParam String id) {
        String token = authTokenService.create("another", id, agentType);
        return token;
    }

    @GetMapping("/logout")
    @AuthenticationRequired("another")
    public String logout(@RequestAuthenticationToken String token, @RequestAgentType String agentType) {
        authTokenService.remove(token, agentType);
        return "OK";
    }

}
